﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Security;
using SalaDeEnsayo.WebApplication.SecurityAttributes.AuthorizationAttributes;

using System.Text;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Script.Serialization;
using System.ComponentModel;
using System.Reflection;


namespace SalaDeEnsayo.WebApplication.Pages
{
    public class BasePage : Page, IPostBackEventHandler
    {
        #region Constantes
        public const string POPUP_HIDDEN_ID = "hidPopupReturnObject";
        public const int DEFAULT_POPUP_WIDTH = 500;
        public const int DEFAULT_POPUP_HEIGHT = 300;
        public const string SCRIPT_FULLNAME = "SalaDeEnsayo.WebApplication.Scripts.PopUpScript.js";
        public const string POPUP_SCRIPT_KEY = "OpenPopup";
        public const string EVENT_POPUP_SUCCESS_KEY = "OnPopupSuccess";
        public const string EVENT_POPUP_CANCEL_KEY = "OnPopupCancel";
        public const string EVENT_POPUP_CLOSE_KEY = "OnPopupClose";
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (ScriptManager.GetCurrent(Page) != null)
                ScriptManager.RegisterClientScriptResource(this, typeof(BasePage), SCRIPT_FULLNAME);
            else
                Page.ClientScript.RegisterClientScriptResource(typeof(BasePage), SCRIPT_FULLNAME);
        }

        protected override void OnLoad(EventArgs e)
        {
            try
            {
                
                #region [ Controlo la seguridad ]

                //string[] roles;
                //if (!AuthorizationRolePrincipal.TryGetRoles(out roles) && Roles.Enabled)
                //    roles = Roles.GetRolesForUser();
                //roles = roles ?? new string[0];

                Type pageType = Page.GetType().BaseType;

                // Obtengo los atributos de método y servicio correspondientes a la tarea
                //RolPermisoEjecucionAttribute pageRoleAttr = Attribute.GetCustomAttribute(pageType, typeof(RolPermisoEjecucionAttribute)) as RolPermisoEjecucionAttribute;
                var pageRoleAttrs = Attribute.GetCustomAttributes(pageType, typeof(RolPermisoEjecucionAttribute)).Cast<RolPermisoEjecucionAttribute>();

                // Según los atributos valido que tenga permiso para la tarea
                if (pageRoleAttrs != null && pageRoleAttrs.Any() && !pageRoleAttrs.Any(p => this.User.IsInRole(p.NombreRol)))
                    throw new SecurityException(SalaDeEnsayo.WebApplication.Properties.Resources.PageExecutionAuthorization_Unauthorized);

                #endregion

                base.OnLoad(e);
            }
            catch (System.Threading.ThreadAbortException ex)
            {
                var threadAbortException = ex;
                //Error no reconocible en ciertos requuest. 
                //El stack trace muestra que es de un método interno de ASP.Net
                //No lo registramos.
            }
            catch (Exception oldEx)
            {
                Response.Redirect("Default.aspx");
            }
        }

        #region IPostBackEventHandler Members

        public void RaisePostBackEvent(string eventArgument)
        {
            
        }

        #endregion
    }
}
